home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / src / GLperf3.12-src.lha / GLperf / Tris.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-01  |  6.9 KB  |  205 lines

  1. /*
  2. //   (C) COPYRIGHT International Business Machines Corp. 1993
  3. //   All Rights Reserved
  4. //   Licensed Materials - Property of IBM
  5. //   US Government Users Restricted Rights - Use, duplication or
  6. //   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. //
  8.  
  9. //
  10. // Permission to use, copy, modify, and distribute this software and its
  11. // documentation for any purpose and without fee is hereby granted, provided
  12. // that the above copyright notice appear in all copies and that both that
  13. // copyright notice and this permission notice appear in supporting
  14. // documentation, and that the name of I.B.M. not be used in advertising
  15. // or publicity pertaining to distribution of the software without specific,
  16. // written prior permission. I.B.M. makes no representations about the
  17. // suitability of this software for any purpose.  It is provided "as is"
  18. // without express or implied warranty.
  19. //
  20. // I.B.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  21. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL I.B.M.
  22. // BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  23. // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  24. // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  25. // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  26. //
  27. // Author:  John Spitzer, IBM AWS Graphics Systems (Austin)
  28. //
  29. */
  30.  
  31. #include <math.h>
  32. #include "Tris.h"
  33.  
  34. #define sq(val) ((val)*(val))
  35.  
  36. #if !defined(max)
  37.  #define max(a,b) ((a)>(b)?(a):(b))
  38. #endif
  39.  
  40. #undef offset
  41. #define offset(v) offsetof(Triangles,v)
  42.  
  43. static InfoItem TrianglesInfo[] = {
  44. #define INC_REASON INFO_ITEM_ARRAY
  45. #include "Tris.h"
  46. #undef INC_REASON
  47. };
  48. #include <malloc.h>
  49.  
  50. TrianglesPtr new_Triangles()
  51. {
  52.     TrianglesPtr this = (TrianglesPtr)malloc(sizeof(Triangles));
  53.     CheckMalloc(this);
  54.     new_Polygonal((PolygonalPtr)this);
  55.     SetDefaults((TestPtr)this, TrianglesInfo);
  56.     this->testType = TrianglesTest;
  57.     this->primitiveType = GL_TRIANGLES;
  58.     this->vertsPerFacet = 3;
  59.     this->usecPixelPrint = " microseconds per pixel with disjoint Triangles";
  60.     this->ratePixelPrint = " pixels per second with disjoint Triangles";
  61.     this->usecPrint = " microseconds per Triangle in a disjoint Triangles call";
  62.     this->ratePrint = " Triangles per second in a disjoint Triangles call";
  63.     /* Set virtual functions */
  64.     this->delete = delete_Triangles;
  65.     this->Layout = Triangles__Layout;
  66.     this->Copy = Triangles__Copy;
  67.     return this;
  68. }
  69.  
  70. void delete_Triangles(TestPtr thisTest)
  71. {
  72.     TrianglesPtr this = (TrianglesPtr)thisTest;
  73.     delete_Polygonal(thisTest);
  74. }
  75.  
  76. TestPtr Triangles__Copy(TestPtr thisTest)
  77. {
  78.     TrianglesPtr this = (TrianglesPtr)thisTest;
  79.     TrianglesPtr newTriangles = new_Triangles();
  80.     FreeStrings((TestPtr)newTriangles);
  81.     *newTriangles = *this;
  82.     CopyStrings((TestPtr)newTriangles, (TestPtr)this);
  83.     return (TestPtr)newTriangles;
  84. }
  85.  
  86. void Triangles__Layout(VertexPtr thisVertex)
  87. {
  88.     TrianglesPtr this = (TrianglesPtr)thisVertex;
  89.     const double pi=3.141592654;
  90.     double ndcSize, width, height, radius0, radius1;
  91.     double theta, theta0, deltaTheta;
  92.     GLfloat x, y;
  93.     GLfloat *src, *dst;
  94.     GLfloat *newTraversalData;
  95.     int numFrontIn, numFrontOut, numFrontClip;
  96.     int numBackIn, numBackOut, numBackClip;
  97.     int frontIn, frontOut, frontClip, backIn, backOut, backClip;
  98.     GLfloat face=1.0;
  99.     int i, j;
  100.     GLfloat facingFront = this->facingFront;
  101.     GLfloat facingBack  = this->facingBack;
  102.     GLfloat numObjects  = this->numObjects;
  103.     GLfloat acceptObjs  = this->acceptObjs;
  104.     GLfloat rejectObjs  = this->rejectObjs;
  105.     GLfloat clipObjs     = this->clipObjs;
  106.     int windowDim = min(this->environ.windowWidth, this->environ.windowHeight);
  107.  
  108.     /* Use Vertex__Layout() and Vertex__orientation to choose layout */
  109.     this->vertsPerBgnEnd = this->objsPerBgnEnd * this->vertsPerFacet;
  110.     this->facetsPerBgnEnd = this->objsPerBgnEnd;
  111.  
  112.     /* Figure front facing/back facing stuff */
  113.     if (fabs(1.0 - facingFront - facingBack) > .01) {
  114.         printf("FrontFacing + BackFacing not equal to 1\n");
  115.     exit(1);
  116.     }
  117.     numFrontIn = floor((GLfloat)numObjects * acceptObjs * facingFront + 0.5);
  118.     numFrontOut = floor((GLfloat)numObjects * rejectObjs * facingFront + 0.5);
  119.     numFrontClip = floor((GLfloat)numObjects * clipObjs * facingFront + 0.5);
  120.     numBackIn = floor((GLfloat)numObjects * acceptObjs * facingBack + 0.5);
  121.     numBackOut = floor((GLfloat)numObjects * rejectObjs * facingBack + 0.5);
  122.     numBackClip = floor((GLfloat)numObjects * clipObjs * facingBack + 0.5);
  123.     numFrontIn += numObjects - numFrontIn - numFrontOut - numFrontClip
  124.                              - numBackIn - numBackOut - numBackClip;
  125.     frontIn = frontOut = frontClip = backIn = backOut = backClip = 0;
  126.  
  127.     /* Figure polygon dimensions given polygon size in pixels and number of sides */
  128.     ndcSize = this->size/(double)windowDim/(double)windowDim*4.0;
  129.     width = sqrt(2.0*ndcSize/this->aspect);
  130.     height = 2.0*ndcSize/width;
  131.     radius0 = sqrt(sq(height/3.0) + sq(width/2.0));
  132.     radius1 = 2.0*height/3.0;
  133.     deltaTheta = pi - atan(3.0*width/2.0/height);
  134.  
  135.     /* Use Vertex__Layout() to get polygon centers */
  136.     this->layoutPoints = numObjects;
  137.     this->layoutPadding = max(radius0,radius1);
  138.     this->layoutPadding += (1. + (this->antiAlias!=Off))/
  139.                            (float)windowDim;
  140.     Vertex__Layout(thisVertex);
  141.  
  142.     /* Allocate necessary data */
  143.     src = this->traversalData;
  144.     newTraversalData = (GLfloat*)malloc(numObjects * 2 * 3 * sizeof(GLfloat));
  145.     CheckMalloc(newTraversalData);
  146.     dst = newTraversalData;
  147.  
  148.     /* Figure initial theta position for orientation */
  149.     switch (this->orientation) {
  150.     case Horizontal:
  151.         theta0 = -deltaTheta + pi/2.0;
  152.         break;
  153.     case Vertical:
  154.         theta0 = -deltaTheta;
  155.         break;
  156.     case Random:
  157.             mysrand(15000);
  158.         break;
  159.     }
  160.  
  161.     /* Generate those vertices around the point centers */
  162.     for (i=0; i<numObjects; i++) {
  163.     x = *src++;
  164.     y = *src++;
  165.     if (this->orientation == Random)
  166.             theta0 = 2.0*pi*(double)myrand()/(double)MY_RAND_MAX;
  167.     if ((x==-1 || x==1) && (-1<=y && y<=1) || (y==-1 || y==1) && (-1<=x && x<=1)) {
  168.         /* The vertex is clipped (on the viewport boundary) */
  169.         if (frontClip < numFrontClip) {
  170.         frontClip++;
  171.         face = 1.0;
  172.         } else {
  173.         backClip++;
  174.         face = -1.0;
  175.         }
  176.     } else if (-1<=x && x<=1 && -1<=y && y<=1) {
  177.         /* The vertex is in the viewport */
  178.         if (frontIn < numFrontIn) {
  179.         frontIn++;
  180.         face = 1.0;
  181.         } else {
  182.         backIn++;
  183.         face = -1.0;
  184.         }
  185.     } else {
  186.         /* The vertex is outside the viewport */
  187.         if (frontOut < numFrontOut) {
  188.         frontOut++;
  189.         face = 1.0;
  190.         } else {
  191.         backOut++;
  192.         face = -1.0;
  193.         }
  194.     }
  195.     theta = face*theta0;
  196.     for (j=0; j<3; j++) {
  197.         *dst++ = ((j&1) ? radius1 : radius0) * cos(theta) + x;
  198.         *dst++ = ((j&1) ? radius1 : radius0) * sin(theta) + y;
  199.         theta += face*deltaTheta;
  200.     }
  201.     }
  202.     free(this->traversalData);
  203.     this->traversalData = newTraversalData;
  204. }
  205.